﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using System.IO;
using System.Net;

namespace YXShop.Web.admin.product
{
    public partial class taobao_info : System.Web.UI.Page
    {
        private string sip_sessionid = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["sip_sessionid"] != null)
                {
                    sip_sessionid = Session["sip_sessionid"].ToString();
                }
                else
                {
                    sip_sessionid = Guid.NewGuid().ToString();
                    Session["sip_sessionid"] = sip_sessionid;
                }
                string productID = ChangeHope.WebPage.PageRequest.GetQueryString("productId");
                string nick = ChangeHope.WebPage.PageRequest.GetQueryString("nick");
                this.GetTaoBaoInfo(productID, nick);
            }
        }

        protected void GetTaoBaoInfo(string productID, string nick)
        {

            ChangeHope.Common.TaoBaoAPI myapi = new ChangeHope.Common.TaoBaoAPI();
            HttpWebResponse myReponse = myapi.taobao_items_single_get("iid,input_pids,num,title,nick,type,pic_path,desc,cid,price,type,location.city,list_time ,delist_time,post_fee,valid_thru", productID, nick);
            StreamReader reader = new StreamReader(myReponse.GetResponseStream(), System.Text.Encoding.UTF8);
            string content = reader.ReadToEnd();
            string sip_status = myReponse.GetResponseHeader("sip_status");
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(content);
            if (sip_status == "9999")
            {
                XmlNodeList mylist = xmlDoc.SelectNodes("rsp/item");
                foreach (XmlNode node in mylist)
                {
                    this.lbTitle.Text = node.SelectSingleNode("title").InnerText;
                    this.lbnichen.Text = node.SelectSingleNode("nick").InnerText;
                    this.lbProductType.Text = node.SelectSingleNode("type").InnerText == "fixed" ? "一口价" : "拍卖";
                    this.lbProductBelongs.Text= GetItemList_Type(node.SelectSingleNode("cid").InnerText);
                    this.lbDesc.Text = node.SelectSingleNode("desc").InnerText;
                    this.lbProductNum.Text = node.SelectSingleNode("num").InnerText;
                    this.lbvalid_thru.Text = node.SelectSingleNode("valid_thru").InnerText;
                    this.lbListTime.Text = node.SelectSingleNode("list_time").InnerText;
                    this.lbDownTime.Text = node.SelectSingleNode("delist_time").InnerText;
                    this.lbPrice.Text = node.SelectSingleNode("price").InnerText;
                    this.Label1.Text = node.SelectSingleNode("input_pids").InnerText;
                }
            }

        }

        #region 商品类型
        protected string GetItemList_Type(string cid)
        {
            try
            {
                ChangeHope.Common.TaoBaoAPI myapi = new ChangeHope.Common.TaoBaoAPI();
                HttpWebResponse myReponse = myapi.taobao_itemcats_get_v2("", cid, "cid,parent_cid,name");
                StreamReader reader = new StreamReader(myReponse.GetResponseStream(), System.Text.Encoding.UTF8);
                string content = reader.ReadToEnd();
                string sip_status = myReponse.GetResponseHeader("sip_status");
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(content);
                XmlNode url = xmlDoc.SelectSingleNode("//url");
                //根据各自的返回的地址不同值不同
                string sip_redirecturl = "http://localhost:8222/TaoBaoAPITest/OnSaleList.aspx";
                switch (sip_status)
                {
                    case "1004":
                    case "1005":
                        //divMeg.InnerText = content;
                        Response.Redirect(url.InnerText + "&sip_redirecturl=" + sip_redirecturl);

                        break;
                    case "1001":
                    case "9999":
                        XmlNodeList mylist = xmlDoc.SelectNodes("rsp/item_cat");
                        return AddXmlNodesToTable_Type(mylist);
                        break;
                    default:
                        break;
                }

            }
            catch (Exception ex)
            {
                ChangeHope.WebPage.BasePage.PageError("链接淘宝服务器出错，返回重新链接。", "taobao_import.aspx");
            }
            return string.Empty;
        }
        private string AddXmlNodesToTable_Type(XmlNodeList nodes)
        {
            foreach (XmlNode node1 in nodes)
            {
                return node1.SelectSingleNode("name").InnerText;
            }
            return string.Empty;
        }
        #endregion
    }
}
